package utils

import (
	"fmt"
	"gin-admin/global"
	"gin-admin/model"
	"strconv"
	"strings"
	"sync"
	"sync/atomic"
)

var (
	counter int64
	once    sync.Once
)

func initCounter() {
	var user model.User
	result := global.GA_DB.Order("created_at desc").Last(&user)
	if result.Error == nil {
		counter, _ = strconv.ParseInt(strings.Split(user.UserID, "uid")[1], 10, 64)
	}
}
func GenerateUserID() string {
	once.Do(initCounter)
	newCounter := atomic.AddInt64(&counter, 1)
	return fmt.Sprintf("uid%06d", newCounter)
}
